'use strict';

// Declare app level module which depends on views, and components

//var access_token = "tewdajbntnyp6lbxi3nr4zprk1ua";
var access_token = "";
var lng = "0";
var lat = "0";
var gHttp;
//var isBound = 0;
//var boundPhonelinkStatic = "";

angular.module('myApp', [
  'ngRoute',
  'myApp.borrow',
  'myApp.boundPhone',
  'myApp.nearLaidian',
  'myApp.pickUp',
  'myApp.charge',
  'myApp.cdbRecord',
  'myApp.version'
])
    .config(['$routeProvider','$locationProvider','$httpProvider', function($routeProvider,$locationProvider) {
        //$locationProvider.html5Mode(true);
      $routeProvider.otherwise({redirectTo: '/'});
        //$httpProvider.defaults.timeout = 5000;
    }])

    //配置基础变量
    .value("httpHost","http://mobile-api.imlaidian.com:8089/cdt")
    //.value("access_token","")


    //指令
    //弹窗
    .directive('lPopTips', function() {
        return {
            restrict: 'E',
            transclude: true,
            controller: ['$scope', function($scope) {
                var buttons = $scope.buttons = [];
                this.addButton = function(button) {
                    buttons.push(button);

                    console.log("$scope.buttons.length"+button);
                    if($scope.buttons.length == 2){
                        $scope.poptipsButtonType = "two-button";
                        button.buttonType="two-button-2";
                    }else{
                        $scope.poptipsButtonType = "one-button";
                        button.buttonType="one-button-1";
                    }

                    console.log("$scope.buttons===1"+$scope.buttons);
                };

                //$scope.updatePop = function(){
                //    $scope.poptips = {};
                //    $scope.poptips.title = attrs.title;
                //    $scope.poptips.desc = attrs.desc;
                //    $scope.poptips.img = attrs.img;
                //    $scope.poptips.poptype = attrs.poptype;
                //}

                console.log("$scope.buttons==="+$scope.buttons);
            }],
            link: function(scope, element, attrs) {
                scope.poptips = {};
                scope.poptips.title = attrs.title;
                scope.poptips.desc = attrs.desc;
                scope.poptips.img = attrs.img;
                scope.poptips.poptype = attrs.poptype;
            },
            templateUrl: 'common-html/pop-tips.html'
        };
    })
    .directive('lPopButton', function() {
        return {
            require: '^lPopTips',
            restrict: 'E',
            transclude: true,
            scope: {
            },
            link: function(scope, element, attrs, poptips) {

                poptips.addButton(scope);
            },
            templateUrl: 'common-html/pop-tips-button.html'
        };
    })


    //来电HTTP服务
    .service('lHTTP', function($http,httpHost) { // injectables go here
        //$http({timeout:5});
          this.jsonp = function(url){
            url = httpHost + url+"&access_token="+access_token+"&callback=JSON_CALLBACK&platform=weixin";
            return $http.jsonp(url);
          };

    })


//<l-pop-tips ng-show="showPopTips" poptype="{{popType}}" title="{{popTitle}}" desc="{{popDesc}}" img="{{popImg}}" >
//    <l-pop-button ng-click="clickFunction1()" ng-if="popIf">{{button1}}</l-pop-button>
//<l-pop-button ng-click="clickFunction2()">{{button2}}</l-pop-button>
//</l-pop-tips>
    //来电弹出窗
    .service('lPop',function($timeout) { // injectables go here
        this.popSet = function(popType , $scope ,popTitle ,popDesc , popImg , button1,button1Method , button2 , button2Method,autoHideTime){


            console.log($scope)
            var scope = $scope;
            if(button1 == undefined){
                $scope.popIf = false;
            }else{
                $scope.popIf = true;
            }
            $scope.poptips = {};
            $scope.poptips.poptype = popType;
            $scope.poptips.title = popTitle;
            $scope.poptips.desc = popDesc;
            $scope.poptips.img = popImg;
            $scope.button1 = button1;
            $scope.button2 = button2;
            $scope.clickFunction1 = button1Method;

            if(button1 == undefined && button2Method == undefined){
                $scope.clickFunction2 = function(){
                    $scope.showPopTips = false;
                }
            }
            else if(button2 != undefined && button2Method == undefined){
                $scope.clickFunction2 = function(){
                    $scope.showPopTips = false;
                }
            }
            else{
                $scope.clickFunction2 = button2Method;
            }


            if(button1 == undefined ){
                console.log($scope.buttons);
                if($scope.buttons!=undefined && $scope.buttons.length>1){
                    var button = $scope.buttons.pop();
                    var button2 = $scope.buttons.pop();
                    if($scope.buttons.length == 2){
                        $scope.poptipsButtonType = "two-button";
                        button.buttonType="two-button-2";
                    }else{
                        $scope.poptipsButtonType = "one-button";
                        button2.buttonType="one-button-1";
                    }
                    $scope.buttons.push(button2);
                }

            }




            //$scope.updatePop();

            //$lPopTips.updatePop();

            //$.scopeHide = function(){
            //    $scope.showPopTips = false;
            //    console.log("aaaaaa")
            //}
            //
            if(autoHideTime != undefined){
                $timeout(function(){ $scope.showPopTips = false;},autoHideTime);
            }
            $scope.showPopTips = true;
            console.log($scope)
        };


    })


;




